/*
  Tên:Nguyễn Văn Huy
  MSSV:11374891
  Lớp:DHTH7DLT
  Ngày hoàn thành:28/04/2012
  */
#include "frsach.h"
#include "ui_frsach.h"
#include "QMessageBox"

frSach::frSach(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frSach)
{
    ui->setupUi(this);
    DB=new MySqlConnection();
    loadcbbox();
    loadcbtl();
    loadsach();
    resetTableSelectionEvent();
    connect(ui->bthienthi,SIGNAL(clicked()),this,SLOT(themsach()));
    connect(ui->btxoa,SIGNAL(clicked()),this,SLOT(xoasach()));
    connect(ui->btsua,SIGNAL(clicked()),this,SLOT(suasach()));
}

frSach::~frSach()
{
    delete ui;
}

void frSach::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
        switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
        }
}

void frSach::loadcbbox()
{
    DB->Open();
    QSqlQuery query(DB->DB);
        query.prepare("SELECT TenTacGia,MaTacGia FROM TacGia");
        query.exec();

        //QMessageBox::information(this,"teast",query.value(0).toString());
        for(int i=0;i<query.size();i++)
        {
            query.next();
            ui->cbtacgia->addItem(query.value(0).toString(),query.value(1).toInt(0));
        }
}

void frSach::themsach()
{
    DB->Open();
    int valuetg,idtg,valuetl,idtl,soluongton,dongia;
    QString tensach;
    idtg=ui->cbtacgia->currentIndex();
    idtl=ui->cbtheloai->currentIndex();
    valuetl=ui->cbtheloai->itemData(idtl).toInt(0);
    valuetg=ui->cbtacgia->itemData(idtg).toInt(0);
    if(ui->txtten->text().isEmpty()||ui->txtgia->text().isEmpty()||ui->txtsoluong->text().isEmpty())
    {
        QMessageBox::information(this,"Thông báo","Bạn cần nhập đủ thông tin");
        return;
    }
    tensach=ui->txtten->text();
    soluongton=ui->txtsoluong->text().toInt(0,10);
    dongia=ui->txtgia->text().toInt(0,10);
    QSqlQuery insertQuery(DB->DB);
    insertQuery.prepare("INSERT INTO Sach (TenSach, GiaBan, SoLuongTon, MaTacGia, MaTheLoai) VALUES (:TenSach, :GiaBan, :SoLuongTon, :MaTacGia, :MaTheLoai)");
    insertQuery.bindValue(":TenSach",tensach);
    insertQuery.bindValue(":GiaBan",dongia);
    insertQuery.bindValue(":SoLuongTon",soluongton);
    insertQuery.bindValue(":MaTacGia",valuetg);
    insertQuery.bindValue(":MaTheLoai",valuetl);
    if(!insertQuery.exec())
    {
         QMessageBox::critical(this,"Ilegal string",insertQuery.lastError().text());
    }
     QMessageBox::information(this,"Success","Successfully Entered to Database");
     ui->txtten->clear();
     ui->txtgia->clear();
     ui->txtsoluong->clear();
     DB->Close();
    //QMessageBox::information(this,"teast",QString::number(valuetg).toStdString().c_str());
}

void frSach::xoasach()
{
    if(selecteds.isEmpty())
    {
        QMessageBox::information(this,"thông báo","chọn một id trên bảng sách để xóa");
        return;
    }
    if(QMessageBox::question(this, "Xóa Sách", "Bạn có muốn xóa sách [" + selecttens + "]?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
    {
                    return;
    }
    DB->Open();
    QSqlQuery deletetheloai(DB->DB);
    deletetheloai.prepare("DELETE FROM Sach WHERE MaSach=?");
    deletetheloai.bindValue(0,selecteds);
    deletetheloai.exec();
    resetTableSelectionEvent();
    //QMessageBox::information(this,"xoa id","ban muon xoa id"+selectedtl);
    DB->Close();
}

void frSach::suasach()
{
    if(selecteds.isEmpty())
    {
        QMessageBox::information(this,"thông báo","chọn một id trên bảng thể loại để sửa");
        return;
    }
    if(QMessageBox::question(this, "Sửa Thể Loại", "Bạn có muốn sửa thể loại '" + selecttens + "' thành '"+ ui->txtten->text() + "'?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
    {
                    return;
    }
    DB->Open();
    int idtg=ui->cbtacgia->currentIndex();
    int idtl=ui->cbtheloai->currentIndex();
    int valuetl=ui->cbtheloai->itemData(idtl).toInt(0);
    int valuetg=ui->cbtacgia->itemData(idtg).toInt(0);
    QSqlQuery Updatetl(DB->DB);
    Updatetl.prepare("UPDATE Sach SET TenSach= ? ,GiaBan= ?,SoLuongTon= ?,MaTacGia= ?,MaTheLoai=? WHERE MaSach= ?");
    Updatetl.bindValue(0,ui->txtten->text());
    Updatetl.bindValue(1,ui->txtgia->text());
    Updatetl.bindValue(2,ui->txtsoluong->text());
    Updatetl.bindValue(3,valuetg);
    Updatetl.bindValue(4,valuetl);
    Updatetl.bindValue(5,selecteds);
    Updatetl.exec();
    resetTableSelectionEvent();
    DB->Close();
}

void frSach::loadcbtl()
{
    DB->Open();
    QSqlQuery query(DB->DB);
        query.prepare("SELECT TenTheLoai,MaSach FROM Sach  where MaTheLoai=0");
        query.exec();

        //QMessageBox::information(this,"teast",query.value(0).toString());
        for(int i=0;i<query.size();i++)
        {
            query.next();
            ui->cbtheloai->addItem(query.value(0).toString(),query.value(1).toInt(0));
        }

}

void frSach::loadsach()
{
    DB->Open();
    QSqlQueryModel *model = new QSqlQueryModel();
    model->setQuery("SELECT * FROM QLBSach.Sach where MaTheLoai !=0 order by MaSach desc  ", DB->DB);
    ui->tbsach->setModel(model);
    ui->tbsach->show();
    DB->Close();
}

void frSach::resetTableSelectionEvent()
{
    loadsach();
    selectionModel=ui->tbsach->selectionModel();
    connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(selectRow()));
}

void frSach::selectRow()
{
     QAbstractItemModel *item =ui->tbsach->model();
     QModelIndex index=ui->tbsach->currentIndex();
     int rowIndex=index.row();
     ui->txtten->setText(item->data(item->index(rowIndex,1)).toString());
     ui->txtgia->setText(item->data(item->index(rowIndex,2)).toString());
     ui->txtsoluong->setText(item->data(item->index(rowIndex,3)).toString());
     selecteds=item->data(item->index(rowIndex,0)).toString();
     selecttens=item->data(item->index(rowIndex,1)).toString();
     int cbboxtl=item->data(item->index(rowIndex,5)).toInt(0);
         int indextl=ui->cbtheloai->findData(cbboxtl);
         int indextg=ui->cbtacgia->findData(item->data(item->index(rowIndex,4)).toInt(0));
         if(indextl!=-1)
         {
             ui->cbtheloai->setCurrentIndex(indextl);
         }
         if(indextg!=-1)
         {
             ui->cbtacgia->setCurrentIndex(indextg);
         }

}
